//
//  CJExten+UILabel.swift
//  CJCore
//
//  Created by 陈警卫 on 2024-07-03.
//

import UIKit
import Foundation

public extension UILabel {
    
    /// 快速创建UILabel
    @objc static func cj_label(frame: CGRect = .zero,
                               font: UIFont = .systemFont(ofSize: 16),
                               textColor: UIColor = .black,
                               text: String? = nil,
                               numberOfLines: Int = 1,
                               textAlignment: NSTextAlignment = .left) -> UILabel {
        let label = UILabel(frame: frame)
        label.font = font
        label.text = text
        label.textColor = textColor
        label.numberOfLines = numberOfLines
        label.textAlignment = textAlignment
        return label
    }
    
    /// 快速创建UILabel
    @objc static func cj_label(font: UIFont = UIFont.systemFont(ofSize: 16),
                               textColor: UIColor = .black) -> UILabel {
        let label = cj_label(frame: .zero, font: font, textColor: textColor)
        return label
    }
    
    /// 设置font
    @objc func cj_font(_ font: UIFont) -> UILabel {
        self.font = font
        return self
    }
    
    /// 设置text
    @objc func cj_text(_ text: String) -> UILabel {
        self.text = text
        return self
    }
    
    /// 设置textColor
    @objc func cj_textColor(_ textColor: UIColor) -> UILabel {
        self.textColor = textColor
        return self
    }
    
    /// 设置numberOfLines
    @objc func cj_numberOfLines(_ numberOfLines: Int) -> UILabel {
        self.numberOfLines = numberOfLines
        return self
    }
    
    /// 设置textAlignment
    @objc func cj_textAlignment(_ textAlignment: NSTextAlignment) -> UILabel {
        self.textAlignment = textAlignment
        return self
    }
    
    /// 获取自适应宽度
    @objc var cj_widthFits: CGFloat {
        let size = self.sizeThatFits(CGSize(width: CGFloat(MAXFLOAT), height: self.cj_height))
        return ceil(size.width)
    }
    
    /// 获取自适应高度
    @objc var cj_heightFits: CGFloat {
        let size = self.sizeThatFits(CGSize(width: self.cj_width, height: CGFloat(MAXFLOAT)))
        return ceil(size.height)
    }
    
    /// 获取自适应尺寸
    @objc var cj_sizeFits: CGSize {
        let size = self.sizeThatFits(CGSize(width: CGFloat(MAXFLOAT), height: CGFloat(MAXFLOAT)))
        return size
    }
    
}
